package com.joe.demo.nio.buffer;

import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;

@Slf4j
public class TestByteBufferReadWrite {

    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(10);
        // a
        buffer.put((byte) 0x61);
        buffer.flip();
        log.info("读取到的数据: {}", (char) buffer.get());
        buffer.clear();
        buffer.put(new byte[]{0x62, 0x63, 0x64});
        buffer.flip();
        log.info("读取到的数据: {}, {}", (char) buffer.get(),  (char) buffer.get());
        buffer.compact();
        log.info("读写指针的位置: {}", buffer.position());
        buffer.put(new byte[]{0x65, 0x6f});
        buffer.rewind();
//        log.info("读取到的数据: {}, {}, {}", (char) buffer.get(0), (char) buffer.get(1), (char) buffer.get(2));
        log.info("读取到的数据: {}, {}, {}", (char) buffer.get(), (char) buffer.get(), (char) buffer.get());
    }
}
